﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace FasterWPF
{
    public static class GridExt
    {
        public static Grid SetBackgroundColor(this Grid chain, Brush brush)
        {
            Brush newBrush = (brush);
            chain.Background = newBrush;
            return chain;
        }

        public static Grid Initialize(this Grid source, ContainerType containerType)
        {  
               source.Initialize(double.NaN, double.NaN, containerType);
               return source;
        }

        public static Grid Initialize(this Grid source, ContainerType containerType, int NumRows, int NumColumns, double minRowHeight, double colWidthToRepeat, GridUnitType unitTypeToRepeat)
        {
               source.Initialize(double.NaN, double.NaN, minRowHeight, containerType, NumRows, NumColumns, colWidthToRepeat, unitTypeToRepeat);
               return source;
        }

        public static Grid Initialize(this Grid source, ContainerType containerType, int NumRows, int NumColumns, double minRowHeight, double colWidth1, GridUnitType unitType1,
           double colWidth2 = 9999999999D, GridUnitType unitType2 = GridUnitType.Auto,
           double colWidth3 = 9999999999D, GridUnitType unitType3 = GridUnitType.Auto,
           double colWidth4 = 9999999999D, GridUnitType unitType4 = GridUnitType.Auto,
           double colWidth5 = 9999999999D, GridUnitType unitType5 = GridUnitType.Auto,
           double colWidth6 = 9999999999D, GridUnitType unitType6 = GridUnitType.Auto,
           double colWidth7 = 9999999999D, GridUnitType unitType7 = GridUnitType.Auto,
           double colWidth8 = 9999999999D, GridUnitType unitType8 = GridUnitType.Auto,
           double colWidth9 = 9999999999D, GridUnitType unitType9 = GridUnitType.Auto,
           double colWidth10 = 9999999999D, GridUnitType unitType10 = GridUnitType.Auto,
           double colWidth11 = 9999999999D, GridUnitType unitType11 = GridUnitType.Auto,
           double colWidth12 = 9999999999D, GridUnitType unitType12 = GridUnitType.Auto,
           double colWidth13 = 9999999999D, GridUnitType unitType13 = GridUnitType.Auto,
           double colWidth14 = 9999999999D, GridUnitType unitType14 = GridUnitType.Auto,
           double colWidth15 = 9999999999D, GridUnitType unitType15 = GridUnitType.Auto,
           double colWidth16 = 9999999999D, GridUnitType unitType16 = GridUnitType.Auto,
           double colWidth17 = 9999999999D, GridUnitType unitType17 = GridUnitType.Auto,
           double colWidth18 = 9999999999D, GridUnitType unitType18 = GridUnitType.Auto,
           double colWidth19 = 9999999999D, GridUnitType unitType19 = GridUnitType.Auto,
           double colWidth20 = 9999999999D, GridUnitType unitType20 = GridUnitType.Auto,
           double colWidth21 = 9999999999D, GridUnitType unitType21 = GridUnitType.Auto,
           double colWidth22 = 9999999999D, GridUnitType unitType22 = GridUnitType.Auto,
           double colWidth23 = 9999999999D, GridUnitType unitType23 = GridUnitType.Auto,
           double colWidth24 = 9999999999D, GridUnitType unitType24 = GridUnitType.Auto,
           double colWidth25 = 9999999999D, GridUnitType unitType25 = GridUnitType.Auto)
                {
                    source.Initialize(double.NaN, double.NaN, minRowHeight, containerType, NumRows, NumColumns, colWidth1, unitType1,
           colWidth2, unitType2,
           colWidth3, unitType3,
           colWidth4, unitType4,
           colWidth5, unitType5,
           colWidth6, unitType6,
           colWidth7, unitType7,
           colWidth8, unitType8,
           colWidth9, unitType9,
           colWidth10, unitType10,
           colWidth11, unitType11,
           colWidth12, unitType12,
           colWidth13, unitType13,
           colWidth14, unitType14,
           colWidth15, unitType15,
           colWidth16, unitType16,
           colWidth17, unitType17,
           colWidth18, unitType18,
           colWidth19, unitType19,
           colWidth20, unitType20,
           colWidth21, unitType21,
           colWidth22, unitType22,
           colWidth23, unitType23,
           colWidth24, unitType24,
           colWidth25, unitType25);
                    return source;
        }

        public static Grid Initialize(this Grid source, double width, double height, ContainerType containerType)
        {            
            //Force the Aero theme by default to assure the structure of the Listbox
            Uri uri = new Uri("/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml", UriKind.Relative);
            source.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);

            source.Width = width;
            source.Height = height;
            source.SetContainerType(containerType);
            IComposite icomp = CompositeTypeHelper.CreateIComposite<Grid>(source);
            source.SetIComposite<Grid>(icomp);

            GridParent gbsr = new GridParent();
            source.StoreSetting<Grid>("IParent", gbsr);

            source.AddItemBorder(Brushes.Gray, new Thickness(1, 1, 1, 1)); //default Border     
            source.StoreSetting<Grid>("BorderSettings", new BorderSettings() { Width = double.NaN, Height = double.NaN, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(0, 0, 0, 0), CornerRadius = new CornerRadius(0), Padding = new Thickness(0) });

            return source;
        }

        public static Grid Initialize(this Grid source, double width, double height, double minRowHeight, ContainerType containerType, int NumRows, int NumColumns, double colWidthToRepeat, GridUnitType unitTypeToRepeat, double rowHeightToRepeat, GridUnitType rowUnitTypeToRepeat, double firstRowHeightOverride, GridUnitType firstRowUnitTypeToRepeat)
        {
            source.Initialize(width, height, containerType);

            IterHelpers.ApplyLoop(NumRows, (i =>
            {
                if (i == 0)
                {
                    source.RowDefinitions.Add(new RowDefinition() { MinHeight = minRowHeight, Height = new GridLength(firstRowHeightOverride, firstRowUnitTypeToRepeat) });
                }
                else
                {
                    source.RowDefinitions.Add(new RowDefinition() { MinHeight = minRowHeight, Height = new GridLength(rowHeightToRepeat, rowUnitTypeToRepeat) });
                }
            }));

            IterHelpers.ApplyLoop(NumColumns, (i =>
            {
                ColumnDefinition c = new ColumnDefinition();
                c.Width = new GridLength(colWidthToRepeat, unitTypeToRepeat);
                source.ColumnDefinitions.Add(c);
            }));

            return source;
        }
            
        public static Grid Initialize(this Grid source, double width, double height, double minRowHeight, ContainerType containerType, int NumRows, int NumColumns, double colWidthToRepeat, GridUnitType unitTypeToRepeat, double rowHeightToRepeat, GridUnitType rowUnitTypeToRepeat)
        {
            source.Initialize(width, height, containerType);

            IterHelpers.ApplyLoop(NumRows, (i =>
            {
                source.RowDefinitions.Add(new RowDefinition() { MinHeight = minRowHeight, Height = new GridLength(rowHeightToRepeat, rowUnitTypeToRepeat) });
            }));

            IterHelpers.ApplyLoop(NumColumns, (i =>
            {
                ColumnDefinition c = new ColumnDefinition();
                c.Width = new GridLength(colWidthToRepeat,unitTypeToRepeat);
                source.ColumnDefinitions.Add(c);
            }));

            return source;
        }

        public static Grid Initialize(this Grid source, double width, double height, double minRowHeight, ContainerType containerType, int NumRows, int NumColumns, double colWidthToRepeat, GridUnitType unitTypeToRepeat)
        {
            source.Initialize(width, height, containerType);

            IterHelpers.ApplyLoop(NumRows, (i =>
            {
                source.RowDefinitions.Add(new RowDefinition() { MinHeight = minRowHeight });
            }));

            IterHelpers.ApplyLoop(NumColumns, (i =>
            {
                ColumnDefinition c = new ColumnDefinition();
                c.Width = new GridLength(colWidthToRepeat,unitTypeToRepeat);
                source.ColumnDefinitions.Add(c);
            }));

            return source;
        }

        public static Grid Initialize(this Grid source, double width, double height, double minRowHeight, ContainerType containerType, int NumRows, int NumColumns, double colWidth1, GridUnitType unitType1,
           double colWidth2 = 9999999999D, GridUnitType unitType2 = GridUnitType.Auto,
           double colWidth3 = 9999999999D, GridUnitType unitType3 = GridUnitType.Auto,
           double colWidth4 = 9999999999D, GridUnitType unitType4 = GridUnitType.Auto,
           double colWidth5 = 9999999999D, GridUnitType unitType5 = GridUnitType.Auto,
           double colWidth6 = 9999999999D, GridUnitType unitType6 = GridUnitType.Auto,
           double colWidth7 = 9999999999D, GridUnitType unitType7 = GridUnitType.Auto,
           double colWidth8 = 9999999999D, GridUnitType unitType8 = GridUnitType.Auto,
           double colWidth9 = 9999999999D, GridUnitType unitType9 = GridUnitType.Auto,
           double colWidth10 = 9999999999D, GridUnitType unitType10 = GridUnitType.Auto,
           double colWidth11 = 9999999999D, GridUnitType unitType11 = GridUnitType.Auto,
           double colWidth12 = 9999999999D, GridUnitType unitType12 = GridUnitType.Auto,
           double colWidth13 = 9999999999D, GridUnitType unitType13 = GridUnitType.Auto,
           double colWidth14 = 9999999999D, GridUnitType unitType14 = GridUnitType.Auto,
           double colWidth15 = 9999999999D, GridUnitType unitType15 = GridUnitType.Auto,
           double colWidth16 = 9999999999D, GridUnitType unitType16 = GridUnitType.Auto,
           double colWidth17 = 9999999999D, GridUnitType unitType17 = GridUnitType.Auto,
           double colWidth18 = 9999999999D, GridUnitType unitType18 = GridUnitType.Auto,
           double colWidth19 = 9999999999D, GridUnitType unitType19 = GridUnitType.Auto,
           double colWidth20 = 9999999999D, GridUnitType unitType20 = GridUnitType.Auto,
           double colWidth21 = 9999999999D, GridUnitType unitType21 = GridUnitType.Auto,
           double colWidth22 = 9999999999D, GridUnitType unitType22 = GridUnitType.Auto,
           double colWidth23 = 9999999999D, GridUnitType unitType23 = GridUnitType.Auto,
           double colWidth24 = 9999999999D, GridUnitType unitType24 = GridUnitType.Auto,
           double colWidth25 = 9999999999D, GridUnitType unitType25 = GridUnitType.Auto)
         {
             source.Initialize(width, height, containerType);

            List<double> colWidths = new List<double>();
            List<GridUnitType> colUnitTypes = new List<GridUnitType>();
            colWidths.Add(colWidth1);
            colUnitTypes.Add(unitType1);
            if (colWidth2 != 9999999999D) { colWidths.Add(colWidth2); colUnitTypes.Add(unitType2); }
            if (colWidth3 != 9999999999D) { colWidths.Add(colWidth3); colUnitTypes.Add(unitType3); }
            if (colWidth4 != 9999999999D) { colWidths.Add(colWidth4); colUnitTypes.Add(unitType4); }
            if (colWidth5 != 9999999999D) { colWidths.Add(colWidth5); colUnitTypes.Add(unitType5); }
            if (colWidth6 != 9999999999D) { colWidths.Add(colWidth6); colUnitTypes.Add(unitType6); }
            if (colWidth7 != 9999999999D) { colWidths.Add(colWidth7); colUnitTypes.Add(unitType7); }
            if (colWidth8 != 9999999999D) { colWidths.Add(colWidth8); colUnitTypes.Add(unitType8); }
            if (colWidth9 != 9999999999D) { colWidths.Add(colWidth9); colUnitTypes.Add(unitType9); }
            if (colWidth10 != 9999999999D) { colWidths.Add(colWidth10); colUnitTypes.Add(unitType10); }
            if (colWidth11 != 9999999999D) { colWidths.Add(colWidth11); colUnitTypes.Add(unitType11); }
            if (colWidth12 != 9999999999D) { colWidths.Add(colWidth12); colUnitTypes.Add(unitType12); }
            if (colWidth13 != 9999999999D) { colWidths.Add(colWidth13); colUnitTypes.Add(unitType13); }
            if (colWidth14 != 9999999999D) { colWidths.Add(colWidth14); colUnitTypes.Add(unitType14); }
            if (colWidth15 != 9999999999D) { colWidths.Add(colWidth15); colUnitTypes.Add(unitType15); }
            if (colWidth16 != 9999999999D) { colWidths.Add(colWidth16); colUnitTypes.Add(unitType16); }
            if (colWidth17 != 9999999999D) { colWidths.Add(colWidth17); colUnitTypes.Add(unitType17); }
            if (colWidth18 != 9999999999D) { colWidths.Add(colWidth18); colUnitTypes.Add(unitType18); }
            if (colWidth19 != 9999999999D) { colWidths.Add(colWidth19); colUnitTypes.Add(unitType19); }
            if (colWidth20 != 9999999999D) { colWidths.Add(colWidth20); colUnitTypes.Add(unitType20); }
            if (colWidth21 != 9999999999D) { colWidths.Add(colWidth21); colUnitTypes.Add(unitType21); }
            if (colWidth22 != 9999999999D) { colWidths.Add(colWidth22); colUnitTypes.Add(unitType22); }
            if (colWidth23 != 9999999999D) { colWidths.Add(colWidth23); colUnitTypes.Add(unitType23); }
            if (colWidth24 != 9999999999D) { colWidths.Add(colWidth24); colUnitTypes.Add(unitType24); }
            if (colWidth25 != 9999999999D) { colWidths.Add(colWidth25); colUnitTypes.Add(unitType25); }

            GridSettings gs = new GridSettings()
            {
                NumColumns =
                    NumColumns,
                NumRows = NumRows, 
                ColWidths = colWidths, 
                ColUnitTypes =
                    colUnitTypes
            };

            IterHelpers.ApplyLoop(gs.NumRows, (i =>
            {
                source.RowDefinitions.Add(new RowDefinition() { MinHeight = minRowHeight });
            }));

            IterHelpers.ApplyLoop(gs.NumColumns, (i =>
            {
                ColumnDefinition c = new ColumnDefinition();
                c.Width = new GridLength(gs.ColWidths[i],
gs.ColUnitTypes[i]);
                source.ColumnDefinitions.Add(c);
            }));

            return source;
        }


    }//end of class
}//end of namespace
